package com.imdroid.carac.activities;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.imdroid.carac.IMDroid;
import com.imdroid.carac.R;

public class Register1Activity extends BaseActivity implements OnClickListener {
	/**
	 * 用户昵称
	 */
	private EditText etNickname;
	/**
	 * 用户名称
	 */
	private TextView tvUsername;
	/**
	 * 购买日期
	 */
	// private TextView tvPurchaseDate;
	/**
	 * 电话号码
	 */
	private TextView tvMobilephone;
	/**
	 * 车队密码
	 */
	private EditText etAccessCode;
	/**
	 * 下一步按钮
	 */
	private Button btnNext;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register1);

		// 获取控件
		etNickname = (EditText) this.findViewById(R.id.my_et_reg1_nickname);
		tvUsername = (TextView) this.findViewById(R.id.my_tv_reg1_user_name);
		// tvPurchaseDate = (TextView) this
		// .findViewById(R.id.my_tv_reg1_purchase_date);
		tvMobilephone = (TextView) this
				.findViewById(R.id.my_tv_reg1_mobile_phone);
		etAccessCode = (EditText) this.findViewById(R.id.my_et_reg1_accesscode);
		btnNext = (Button) this.findViewById(R.id.my_btn_reg1_next);

		// 设置背景和文字
		// tvUsername.setBackgroundColor(android.graphics.Color.GRAY);
		// tvPurchaseDate.setBackgroundColor(android.graphics.Color.GRAY);
		// tvMobilephone.setBackgroundColor(android.graphics.Color.GRAY);
		btnNext.setOnClickListener(this);

		// tvUsername.setTextColor(Color.BLACK);
		// tvPurchaseDate.setTextColor(Color.BLACK);
		// tvMobilephone.setTextColor(Color.BLACK);

		refresh();
	}

	/**
	 * Refresh the information
	 */
	public void refresh() {

		etNickname.setText(IMDroid.getUser().getNickname());
		tvUsername.setText(this.getString(R.string.text_user_name)
				+ IMDroid.getUser().getUser_name());
		etAccessCode.setText(IMDroid.getUser().getTeam_access_code());

		// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		// if (IMDroid.getUser().getPurchase_date() != null) {
		// tvPurchaseDate.setText(this.getString(R.string.text_purchase_date)
		// + sdf.format(IMDroid.getUser().getPurchase_date()));
		// }
		tvMobilephone.setText(this.getString(R.string.text_mobile_phone)
				+ IMDroid.getUser().getMobile_phone());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	public void onClick(View view) {
		if (view.getId() == btnNext.getId()) {
			// 检查昵称是否输入
			String nickname = etNickname.getText().toString();
			String accessCode = etAccessCode.getText().toString();
			if (nickname == null || nickname.length() <= 0) {
				this.showAlert(this.getString(R.string.alert_input_nickname));
				return;
			}
			// 检查车队密码，是否输入
			if (accessCode == null || accessCode.length() <= 0) {
				AlertDialog.Builder buidler = new AlertDialog.Builder(this);
				buidler.setMessage(R.string.text_reg1_text4);
				buidler.setPositiveButton(R.string.text_reg1_no,
						new DialogInterface.OnClickListener() {
							/*
							 * (non-Javadoc)
							 * 
							 * @see
							 * android.content.DialogInterface.OnClickListener
							 * #onClick(android.content.DialogInterface, int)
							 */
							public void onClick(DialogInterface dialog,
									int which) {
								saveAndNext();
							}
						});
				buidler.setNegativeButton(R.string.text_reg1_yes,
						new DialogInterface.OnClickListener() {
							/*
							 * (non-Javadoc)
							 * 
							 * @see
							 * android.content.DialogInterface.OnClickListener
							 * #onClick(android.content.DialogInterface, int)
							 */
							public void onClick(DialogInterface dialog,
									int which) {
								etAccessCode.requestFocus();
							}
						});
				buidler.show();
				return;
			}
			// 保存到share preference,并跳转到下一页
			saveAndNext();

		}
	}

	/**
	 * Save the information to share preference
	 */
	private void saveAndNext() {
		// 保存到share preference
		IMDroid.getUser().setNickname(etNickname.getText().toString());
		IMDroid.getUser()
				.setTeam_access_code(etAccessCode.getText().toString());
		// 跳转到下一页
		Intent intent = new Intent();
		intent.setClass(Register1Activity.this, Register3Activity.class);
		startActivity(intent);
		this.finish();
	}

}
